import { BasicColumn, FormSchema } from "/@/components/Table"
import { render } from "/@/utils/common/renderUtils"
import { DescItem } from "/@/components/Description/index"
import { listModule } from "../module/module.api"
import { rules } from "/@/utils/helper/validator"
import DictEnum from '/@/enums/dictEnum'
import * as resourceApi from "/@/api/iam/resource"

const isMenu = (values) => values.resourceType == 1

export const columns: BasicColumn[] = [
  {
    title: "id",
    dataIndex: "id",
    width: 20,
    ifShow: false,
  },
  {
    title: "所属服务",
    dataIndex: "serviceSummary",
    width: 120,
  },

  {
    title: "接口名称",
    dataIndex: "apiName",
    width: 120,
  },

  {
    title: "接口概要",
    dataIndex: "apiSummary",
    width: 120,
  },


  {
    title: "接口路径",
    dataIndex: "url",
    width: 160,
  },

  {
    title: "鉴权操作",
    dataIndex: "apiAction",
    width: 200,
  },

  {
    title: "状态",
    dataIndex: "status",
    width: 90,
    customRender: ({ text, record }) => {
      const color = record.status == 1 ? "green" : "red"
      return render.renderTag(render.renderDict(text, DictEnum.ApiStatus), color)
    },
  },

  {
    title: "操作类型",
    dataIndex: "actionType",
    width: 90,
    customRender: ({ text }) => {
      const color = "green"
      return render.renderTag(render.renderDict(text, "ram_resource_action_type"), color)
    },
  },

  {
    title: "可见范围",
    dataIndex: "showScope",
    width: 90,
    customRender: ({ text }) => {
      const color = "green"
      return render.renderTag(render.renderDict(text, DictEnum.ResourceShowScope), color)
    },
  },

  {
    title: "创建时间",
    dataIndex: "createdAt",
    sorter: true,
    width: 150,
  },
  // {
  //   title: '状态',
  //   dataIndex: 'status',
  //   width: 50,
  //   customRender: ({ text }) => {
  //     const color = text === '1' ? 'green' : 'red';
  //     return render.renderTag(render.renderDict(text, 'tenant_status'), color)
  //   }
  // },
]

export const instantceColumns: BasicColumn[] = [
  {
    title: "id",
    dataIndex: "id",
    width: 20,
    ifShow: false,
  },
  {
    title: "服务名称",
    helpMessage: "在Nacos登录注册中心中注册的服务名称",
    dataIndex: "serviceName",
    width: 120,
  },
  {
    title: "服务概要",
    dataIndex: "serviceSummary",
    width: 120,
  },
  {
    title: "Iam标识",
    dataIndex: "iamCode",
    helpMessage: "目前用于资源鉴权操作前缀, eg: iam:GetUser",
    width: 120,
  },
  {
    title: "系统服务",
    dataIndex: "builtInFlag",
    width: 120,
    customRender: ({ text }) => {
      const color = text ? "green" : "cyan"
      return render.renderTag(text ? '是' : '否', color)
    },
  },

  {
    title: "创建时间",
    dataIndex: "createdAt",
    width: 150,
  }
]

export const instantceSearchFormSchema: FormSchema[] = [
  {
    field: "keyword",
    label: "",
    component: "Input",
    componentProps: {
      placeholder: "服务名/概要",
      allowClear: true,
      showSearch: true
    },
    colProps: { span: 8 },
  }
]

export const searchFormSchema: FormSchema[] = [
  {
    field: "iamCode",
    label: "所属服务",
    component: "ApiSelect",
    componentProps: {
      api: resourceApi.listAllInstance,
      placeholder: "请选择",
      showSearch: true,
      optionFilterProp: "label",
      resultField: "data",
      labelField: "serviceSummary",
      valueField: "iamCode"
    },
    colProps: { span: 8 },
  },
  {
    field: "apiName",
    label: "接口名称",
    component: "Input",
    colProps: { span: 8 },
  },
  {
    field: "actionType",
    label: "动作类型",
    component: "JDictSelectTag",
    componentProps: {
      showSearch: true,
      dictType: "ram_resource_action_type",
      placeholder: "请选择动作类型",
    },
    colProps: { span: 8 },
  },
  {
    field: "showScope",
    label: "可见范围",
    component: "JDictSelectTag",
    componentProps: {
      showSearch: true,
      allowClear: true,
      dictType: DictEnum.ResourceShowScope,
      placeholder: "请选择动作类型",
    },
    colProps: { span: 8 },
  },
  {
    field: "status",
    label: "状态",
    component: "JDictSelectTag",
    componentProps: {
      showSearch: true,
      allowClear: true,
      dictType: DictEnum.ApiStatus,
      placeholder: "全部",
      chooseOptionLabel: "全部"
    },
    colProps: { span: 8 },
  },
  {
    label: '资源类型',
    field: 'resourceType',
    component: "JDictSelectTag",
    componentProps: {
      dictType: DictEnum.IamResourceType,
      placeholder: "全部",
      chooseOptionLabel: "全部",
      filter: (item) => {
        return item != "3"
      },
    },
  },
  {
    field: "ignorePermission",
    label: "忽略权限",
    component: "JDictSelectTag",
    componentProps: {
      dictType: DictEnum.BoolValue,
      placeholder: "全部",
      chooseOptionLabel: "全部"
    },
  },
  {
    field: "ignoreLogin",
    label: "忽略登录",
    component: "JDictSelectTag",
    componentProps: {
      dictType: DictEnum.BoolValue,
      placeholder: "全部",
      chooseOptionLabel: "全部"
    },
  },
  {
    field: "ignoreTenant",
    label: "忽略租户",
    component: "JDictSelectTag",
    componentProps: {
      dictType: DictEnum.BoolValue,
      placeholder: "全部",
      chooseOptionLabel: "全部"
    },
  },
  {
    field: "appHubApi",
    label: "接口范围",
    component: "Select",
    defaultValue: null,
    componentProps: {
      allowClear: true,
      placeholder: "全部",
      options: [
        {
          label: "系统应用",
          value: true,
          key: "system",
        },
        {
          label: "当前应用",
          value: false,
          key: "current",
        },
      ],
    },
  }
]

export const instanceFormSchema: FormSchema[] = [
  {
    field: "id",
    label: "id",
    component: "Input",
    show: false,
    required: false,
  },
  {
    label: "服务概要",
    field: "serviceSummary",
    component: "Input",
    defaultValue: null,
    required: true,
  },
  {
    label: "Iam标识",
    field: "iamCode",
    helpMessage: '接入iam的服务标识，用于资源操作的前缀',
    componentProps: {
      placeholder: "例如: infra",
    },
    dynamicDisabled: ({ values }) => {
      return !!values.id
    },
    component: "Input",
    required: true,
  },
  {
    label: "注册服务名称",
    field: "serviceName",
    helpMessage: '当请求后端接口时候，需要使用，比如 /api/infra-service/getDict',
    componentProps: {
      placeholder: "例如: infra-service",
    },
    component: "Input",
    required: true,
  },
  {
    label: "系统服务",
    field: "builtInFlag",
    dynamicDisabled: ({ values }) => {
      return !!values.id
    },
    helpMessage: '是否为系统服务, 系统服务无需配置host',
    component: "JDictSelectTag",
    defaultValue: '0',
    componentProps: {
      type: "radio",
      dictType: "bool_value",
    },
    required: true,
  },
  {
    label: "主机配置",
    field: "hostList",
    component: "JAddInput",
    ifShow: ({ values }) => {
      return values.builtInFlag === '0'
    },
    componentProps: {
      valuePlaceholder: "权重",
      keyPlaceholder: "IP",
      valueComponentType: "InputNumber",
      isArray: true,
      fieldKeyName: 'host',
      fieldValueName: 'weight',
      valueProps: {
        min: 1,
        max: 120,
        // 限制输入整数
        precision: 0
      }
    },
    required: true,
  }
]

export const formSchema: FormSchema[] = [
  {
    field: "id",
    label: "id",
    component: "Input",
    show: false,
    required: false,
  },
  {
    label: "可见范围",
    field: "showScope",
    component: "JDictSelectTag",
    defaultValue: "1",
    componentProps: {
      placeholder: "请选择",
      allowClear: true,
      showSearch: true,
      type: 'radio',
      dictType: DictEnum.ResourceShowScope,
    },
    required: true,
  },
  {
    label: '资源类型',
    required: true,
    field: 'resourceType',
    component: "JDictSelectTag",
    defaultValue: '2',
    componentProps: {
      type: "radio",
      dictType: DictEnum.IamResourceType,
      filter: (item) => {
        return item != "3"
      },
    },
  },
  {
    field: "ignoreXss",
    label: "忽略Xss",
    helpMessage: '忽略后，将不再进行xss检测',
    required: true,
    defaultValue: false,
    component: "RadioGroup",
    ifShow: ({ values }) => {
      return !isMenu(values)
    },
    componentProps: {
      options: [
        {
          label: "是",
          value: true,
        },
        {
          label: "否",
          value: false,
        },
      ],
    }
  },
  {
    field: "ignorePermission",
    label: "忽略权限",
    helpMessage: '忽略后，只要用户登录就会拥有该接口权限',
    required: true,
    defaultValue: false,
    component: "RadioGroup",
    ifShow: ({ values }) => {
      return !isMenu(values)
    },
    componentProps: {
      options: [
        {
          label: "是",
          value: true,
        },
        {
          label: "否",
          value: false,
        },
      ],
    }
  },
  {
    field: "ignoreLogin",
    label: "忽略登录",
    helpMessage: '如果开启，无需登录即可访问',
    required: true,
    defaultValue: false,
    component: "RadioGroup",
    ifShow: ({ values }) => {
      return !isMenu(values)
    },
    componentProps: {
      options: [
        {
          label: "是",
          value: true,
        },
        {
          label: "否",
          value: false,
        },
      ],
    }
  },
  {
    field: "ignoreTenant",
    label: "忽略租户",
    helpMessage: '如果忽略，不需要传递租户id标识反之必传',
    required: true,
    defaultValue: false,
    component: "RadioGroup",
    ifShow: ({ values }) => {
      return !isMenu(values)
    },
    componentProps: {
      options: [
        {
          label: "是",
          value: true,
        },
        {
          label: "否",
          value: false,
        },
      ],
    }
  },
  {
    field: "iamCode",
    label: "所属服务",
    component: "ApiSelect",
    required: true,
    dynamicDisabled: ({ values }) => {
      return !!values.id
    },
    componentProps: {
      api: resourceApi.listAllInstance,
      showSearch: true,
      placeholder: "请选择",
      optionFilterProp: "label",
      resultField: "data",
      labelField: "serviceSummary",
      valueField: "iamCode"
    }
  },
  {
    label: "接口名称",
    field: "apiName",
    component: "Input",
    required: true,
    dynamicDisabled: ({ values }) => {
      return !!values.id
    },
  },
  {
    label: "接口概要",
    field: "apiSummary",
    component: "Input",
    required: true,
  },
  {
    field: "httpMethod",
    label: "HTTP方法",
    component: "JDictSelectTag",
    defaultValue: 'GET',
    componentProps: {
      showSearch: true,
      dictType: "httpMethod",
      placeholder: "请选择http方法",
    },
    ifShow: ({ values }) => {
      return !isMenu(values)
    },
    required: true,
  },
  {
    field: "actionType",
    label: "操作类型",
    component: "JDictSelectTag",
    componentProps: {
      showSearch: true,
      dictType: "ram_resource_action_type",
      placeholder: "请选择动作类型",
    },
    required: true,
  },
  {
    label: "鉴权操作",
    field: "apiAction",
    component: "Input",
    required: true,
    colProps: { lg: 24, md: 24 },
    ifShow: ({ values }) => {
      return values.apiAction
    },
    dynamicDisabled: true,
  },
  {
    label: "资源路径",
    field: "url",
    component: "Input",
    ifShow: ({ values }) => {
      return !isMenu(values)
    },
    colProps: { lg: 24, md: 24 },
    required: ({ values }) => {
      return !isMenu(values)
    },
  },
  {
    label: "备注",
    field: "remark",
    component: "InputTextArea",
    colProps: { lg: 24, md: 24 },
    componentProps: {
      placeholder: "请输入备注",
      rows: 5,
    },
  },
]

export const formDetailSchema: DescItem[] = [
  {
    field: "id",
    label: "唯一标识",
  },
  {
    field: "serviceName",
    label: "服务名称",
  },
  {
    field: "ignoreTenant",
    label: "忽略租户",
    render: (curVal, data) => {
      const color = "green"
      return render.renderTag(render.renderDict(curVal, "bool_value"), color)
    },
  },
  {
    field: "ignoreLogin",
    label: "忽略授权",
    render: (curVal, data) => {
      const color = "green"
      return render.renderTag(render.renderDict(curVal, "bool_value"), color)
    },
  },

  {
    field: "showScopeName",
    label: "显示范围",
  },
  {
    field: "apiName",
    label: "接口名称",
  },
  {
    field: "apiSummary",
    label: "接口概要",
  },

  {
    field: "url",
    label: "资源路径",
  },

  {
    field: "httpMethod",
    label: "http方法",
  },

  {
    field: "apiAction",
    label: "鉴权操作",
  },

  {
    field: "actionType",
    label: "动作类型",
    render: (curVal, data) => {
      const color = "green"
      return render.renderTag(render.renderDict(curVal, "ram_resource_action_type"), color)
    },
  },

  {
    field: "createdAt",
    label: "创建时间",
  },

  {
    field: "updatedAt",
    label: "更新时间",
  },

  {
    label: "备注",
    labelMinWidth: 60,
    field: "remark",
  },
]

export const authorizeFromSchema: FormSchema[] = [
  {
    label: "权限空间",
    field: "namespaceId",
    component: "ApiSelect",
  },
]
